package overrideoverwrite;

import java.util.ArrayList;

/**
 * @ClassName PersonTest
 * @Date 2024/11/27 17:00 40
 * @Author Jack Chen
 * @Description serenity-cucumber-auto-project-jack
 * @Version 1.0
 */

public class PersonTest {
    public static void main(String[] args) {
        ArrayList<String> hobs = new ArrayList<>();
        hobs.add("Hobby");
        Person person1 = new Person("baili", 20, hobs);


        try {
            Person person2 = person1.clone(); //浅拷贝  或 深拷贝

            System.out.println("Person1: " + person1);
            System.out.println("Person2: " + person2);

            System.out.println("=================================");

            //修改 person2属性
            person2.setName("文斌");
            person2.setAge(30);
            person2.getHobbies().add("sing");  //浅拷贝
            System.out.println("Person1: " + person1);
            System.out.println("Person2: " + person2);
            /** 浅拷贝 output:
             * Person1: Person [name=baili, age=20, hobbies=[Hobby, sing]]
             * Person2: Person [name=文斌, age=30, hobbies=[Hobby, sing]]
             */

            /** 深拷贝 output:
             * Person1: Person [name=baili, age=20, hobbies=[Hobby]]
             * Person2: Person [name=文斌, age=30, hobbies=[Hobby, sing]]
             */
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

}
